package org.bp.ioc.config;

import java.util.HashSet;
import java.util.Set;

import org.bp.ioc.annotation.Controller;
import org.bp.ioc.annotation.Service;
import org.bp.ioc.utils.ClassUtils;

/**
 * 获取包下的类
 */
public final class ClassHelper {
    private static final Set<Class<?>> CLASS_SET;
    static {
        CLASS_SET = ClassUtils.getClassSet(ConfigHelper.getWEB_BASE_PACKAGE());
    }

    /**
     * 获取所有的类
     * @return
     */
    public static Set<Class<?>> getAllClassSet(){
        return CLASS_SET;
    }

    /**
     * 获取Controller注解的所有类
     * @return
     */
    public static Set<Class<?>> getControllerClassSet(){
        Set<Class<?>> classSet = new HashSet<>();
        for (Class<?> clazz : CLASS_SET){
            if(clazz.isAnnotationPresent(Controller.class)){
                classSet.add(clazz);
            }
        }
        return classSet;
    }

    /**
     * 获取Service注解下所有类
     * @return
     */
    public static Set<Class<?>> getServiceClassSet(){
        Set<Class<?>> classSet = new HashSet<>();
        for (Class<?> clazz : CLASS_SET){
            if(clazz.isAnnotationPresent(Service.class)){
                classSet.add(clazz);
            }
        }
        return  classSet;
    }

    /**
     * 获取service Controller 注解的所有bean
     * @return
     */
    public static Set<Class<?>> getBeanClassSet(){
        Set<Class<?>> classSet = new HashSet<>(getControllerClassSet());
        classSet.addAll(getServiceClassSet());
        return  classSet;
    }
}
